<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class RegisterControl extends Control {

    public function user_ajax() {
        if (!IS_AJAX)
            die('非法传参！');
        $user = trim(adds_html($_GET['username']));
        if ($user == '') {
            echo 0;
            die;
        }
        $preg = '/^[0-9a-z]\w{3,12}[0-9a-z]$/i';
        if (!preg_match($preg, $user)) {
            echo 1;
            die;
        }
        $userInfo = M('user')->query("select * from hd_user where username='{$user}'");
        if (!empty($userInfo)) {
            echo 2;
            die;
        }
        echo 3;
    }

    public function pwd_ajax() {
        if (!IS_AJAX)
            die('非法传参！');
        $pwd = trim(adds_html($_GET['passwd']));
        if ($pwd == '') {
            echo 0;
            die;
        }
        $preg = '/^[0-9a-z]\w{3,12}[0-9a-z]$/i';
        if (!preg_match($preg, $pwd)) {
            echo 1;
            die;
        }
        echo 2;
    }

    public function pwded_ajax() {
        if (!IS_AJAX)
            die('非法传参！');
        $pwd = trim(adds_html($_GET['passwd']));
        $pwded = trim(adds_html($_GET['passwded']));
        if ($pwded == '') {
            echo 0;
            die;
        }
        if ($pwd != $pwded) {
            echo 1;
            die;
        }
        echo 2;
    }

    public function code_ajax() {
        if (!IS_AJAX)
            die('非法传参！');
        $code = trim(adds_html($_GET['code']));
        $code = strtoupper($code);
        if ($code != $_SESSION['code']) {
            echo 0;
            die;
        }
        echo 1;
    }

    public function code() {
        $code = new Code();
        $code->show();
    }

    public function register() {
        if (IS_POST) {
            $data = array(
                'username' => adds_html($_POST['username']),
                'passwd' => md5($_POST['pwd']),
                'restime'=>time()
            );
            if (M('user')->add($data)) {
                $this->success('注册成功！', __ROOT__);
            } else {
                $this->error('注册失败！');
            }
        }
    }

}
